<?php

/**
 * 日志
 */
class Logger
{
    static $interface;
    private $monolog;
    static $islog = false;

    // 单例
    static function getInterface($logpath = 'error_log')
    {
        $logpath = strtolower($logpath);
        if (!isset(self::$interface[$logpath]) || is_null(self::$interface[$logpath])) self::$interface[$logpath] = new self($logpath);
        return self::$interface[$logpath];
    }

    function __construct($logpath = 'error_log')
    {
        $path = APPLICATION_PATH . "/cache/{$logpath}/";
        // 确认日志开关
        if (isset(Yaf_Registry::get('config')->logger->status)) self::$islog = Yaf_Registry::get('config')->logger->status;
        $this->monolog = new \Monolog\Logger('logger');
        $this->monolog->pushHandler(new \Monolog\Handler\StreamHandler($path . date('Y-m-d') . '.log', \Monolog\Logger::WARNING));
    }

    /**
     * 一定会写入
     * @param $message
     * @return bool
     */
    function error($message)
    {
        return $this->monolog->error(is_array($message) ? json_encode($message, JSON_UNESCAPED_UNICODE) : $message);
    }

    /**
     * 根据配置写入
     * @param $message
     * @return bool
     */
    function log($message)
    {
        if (self::$islog) return $this->error($message);
    }
}
